/*
$.Class("Product.Grid",
// static properties
{     
},
// prototype properties
    {
        // constructor function
        init: function(id) {
        },
        ... function
})
*/

$.Class("Util",
// static properties
{     
    setEventCheckAll : function(checkAllID, childrenName, callBack) {
	$("#" + checkAllID).change(	
            function() {
		checked_status = this.checked;
		var checkList = $("input[name=" + childrenName + "]").not(
				"#" + checkAllID);

		if (checkList.length === 0) {
			return false;
		} else {
			checkList.each(function() {
				this.checked = checked_status;
			});

			if (typeof (callBack) === "function") {
				callBack();
			}
			return true;
		}
	});
    }
    },
    // prototype properties
    {
        init: function(id) {    }
})
/**************
 * @Description: create range date picker
 * @Params: Object
 *      startId : id của start date picker
 *      endId : id của end date picker
 *      format : (Default: "dd/MM/yyy")
 *      startDate : (Default : now)
 *      endDate : (Default : now)
 */
$.Class("Util.RangeDatePicker",
// static properties
{     
    startChange : function(e) {
        e = e.sender.picker;
        
        var startDate = e.start.value();

        if (startDate) {
            startDate = new Date(startDate);
            startDate.setDate(startDate.getDate());
            e.end.min(startDate);
        }
    },
    endChange : function(e) {
        e = e.sender.picker;

        var endDate = e.end.value();

        if (endDate) {
            endDate = new Date(endDate);
            endDate.setDate(endDate.getDate());
            e.start.max(endDate);
        }
    }
},
// prototype properties
{
    // constructor function
    init: function(o) {
        if(typeof(o) !== "object") o = {};
    
        if(typeof(o.format) !== "string") o.format = "dd/MM/yyyy";
        if(typeof(o.startDate) !== "object") o.startDate = new Date();
        if(typeof(o.endDate) !== "object") o.endDate = new Date();
        if(typeof(o.width) !== "number") o.width = 100;

        this.start = $("#" + o.startId).kendoDatePicker({
            change: Util.RangeDatePicker.startChange,
            value: o.startDate,
            format: o.format
        }).data("kendoDatePicker");
        $("#" + o.startId).closest("span.k-datepicker").width(o.width);
        
        this.end = $("#" + o.endId).kendoDatePicker({
            change: Util.RangeDatePicker.endChange,
            value: o.endDate,
            format: o.format
        }).data("kendoDatePicker");
        $("#" + o.endId).closest("span.k-datepicker").width(o.width);

        this.start.max(this.end.value());
        this.end.min(this.start.value());
        
        this.start.picker = this;
        this.end.picker = this;
    },
})

$.Class("Util.TreeView",
// static properties
{     
},
// prototype properties
    {
        // constructor function
        init: function(id) {
            this.id = id;
            this.dataSource = this.createDataSource();
            this.createTreeView();
        },
        createDataSource: function()
        {
            var dataSource = [{
                id: 1, text: "My Documents", expanded: true, spriteCssClass: "rootfolder", items: [
                    {
                        id: 2, text: "Kendo UI Project", expanded: true, spriteCssClass: "folder", items: [
                            { id: 3, text: "about.html", spriteCssClass: "html" }
                        ]
                    },
                    {
                        id: 6, text: "New Web Site", expanded: true, spriteCssClass: "folder", items: [
                            { id: 7, text: "mockup.jpg", spriteCssClass: "html" },
                            { id: 8, text: "Research.pdf", spriteCssClass: "html" },
                        ]
                    },
                    {
                        id: 9, text: "Reports", expanded: true, spriteCssClass: "folder", items: [
                            { id: 10, text: "February.pdf", spriteCssClass: "folder" , items:[
                                    { id: 16, text: "vh1", spriteCssClass: "html"},
                                    { id: 17, text: "vh2", spriteCssClass: "html"}
                            ]},
                            { id: 11, text: "March.pdf", spriteCssClass: "html" }
                        ]
                    }
                ]
            }];
            
            var arr = [ {id : 0, text : "Root", expanded : true, items : [
                    {
                      id : 1, text : "vh1", expanded : true,
                      items: [
                        { id : 2, text : "vh2" },
                        { id : 3, text : "vh3" },
                        { id : 4, text : "vh4" }
                      ]
                  }, {
                      id : 11, text : "vh11", expanded : true,
                      items: [
                        { id : 12, text : "vh12" },
                        { id : 13, text : "vh13" },
                        { id : 14, text : "vh14" }
                      ]
                  }
                ]}
            ];
            
            var result = $.grep(arr, 
                function(e){ 
                    return e.id == 4; 
                }
            );
            
            result = arr.filter(function (item) { return item.text == "vh1" });
                
            console.log(result);
            
                
            var localDataSource = new kendo.data.HierarchicalDataSource({ data: arr});
            
           // var order = localDataSource.filter({ field: "id", operator: "==", value: 1 });

            //console.log(order);
            
            return localDataSource;
        },
        createTreeView : function()
        {
             $("#" + this.id).kendoTreeView({
                checkboxes: {
                    checkChildren: true,
                    template: "<input type='checkbox' name='checkbox[#= item.id #]' value='#= item.id #' />"
                },
                dataSource: this.dataSource
            });
        },
        getSelectedIds : function()
        {
            var ids = "";
            
            $("input:checked", "#" + this.id).each(function()
            {
                if(ids == "")
                    ids = this.value;
                else
                    ids += "," + this.value;
            });
            
            return ids;
        }
})
